package com.sgb.kjwl.viewmodel.packers;

import android.content.Intent;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;




import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;

import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.packers.TeamOfferDetailActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.SetPwdActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.im.immodel.IMUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author liwei
*@date 2021/1/13
*description:
*/
public class TeamOfferDetailModel extends BaseViewModel {

    private TeamOfferDetailActivity activity;
    private HomeRepertory repertory;

    public TeamOfferDetailModel(TeamOfferDetailActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("workquotion").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&type=0";
                    MLog.d("api", "H5跳转链接  " + url);
                    activity.getUrl(url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void detailOffer(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getDetail(no).enqueue(new Callback<BaseEntity<SelfTeamOfferEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<SelfTeamOfferEntity>> call, Response<BaseEntity<SelfTeamOfferEntity>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<SelfTeamOfferEntity> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.detailOffer(body.getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<SelfTeamOfferEntity>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void detailOfferIm(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getDetail(no, repertory.getAreaCode())
                .enqueue(new Callback<BaseEntity<SelfTeamOfferEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<SelfTeamOfferEntity>> call, Response<BaseEntity<SelfTeamOfferEntity>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<SelfTeamOfferEntity> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.detailEmploymentIm(body.getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<SelfTeamOfferEntity>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void detailTeamTelephone(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getDetail(no, repertory.getAreaCode())
                .enqueue(new Callback<BaseEntity<SelfTeamOfferEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<SelfTeamOfferEntity>> call, Response<BaseEntity<SelfTeamOfferEntity>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.detailTeamTelephone(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<SelfTeamOfferEntity>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void findCustomerServiceImAccount(String imId) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String,Object> map = new HashMap<>();
        map.put("customerServiceContactsId", imId);
        APIManager.getInstance().getPickAPI().getfindCustomerServiceImAccount(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    activity.onBackAccount(response.body().getMsg());
                } else {
                    String dataImId = response.body().getData().toString();
                    IMUtils.getInstance().buildSingleTalking(dataImId);
//                    NimUIKit.startP2PSession(activity, dataImId);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void oneKeyLogin(OneKeyLoginEntity oneKeyLoginEntity) {
        DialogHelper.showProgressDialog(activity, null, "登录中", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().oneClickLogin(oneKeyLoginEntity).enqueue(new Callback<BaseEntity<LoginEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LoginEntity>> call, Response<BaseEntity<LoginEntity>> response) {

                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState() == null) {
                    return;
                }
                if (response.body() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    OneKeyLoginManager.getInstance().finishAuthActivity();
                    if (response.body().getData().isNeedRegister()) {
                        Intent intent = new Intent(activity, SetPwdActivity.class);
                        intent.putExtra("mobile", response.body().getData().getPhone());
                        activity.startActivity(intent);
                    } else {
                        //登录成功逻辑
                        loginImModel(response);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LoginEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 网易IM登陆
     * @param response
     */
    private void loginImModel(Response<BaseEntity<LoginEntity>> response) {
        if (response.body() != null) {
//            NimUIKit.login(new LoginInfo(response.body().getData().getImAccount(), response.body().getData().getImToken()), new RequestCallback<LoginInfo>() {
//                @Override
//                public void onSuccess(LoginInfo param) {
                    DialogHelper.dismissProgressDialog();
                    SharedPreferenceUtil.getInstance().cleatData();
                    saveUserInfo(response.body().getData());
                    MToast.showToast(activity, "登录成功");
                    activity.loginSucceed(response.body().getData(), response.body().getData().getPhone());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, response.body().getData().getToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, response.body().getData().getPhone());
                    JPushInterface.setAlias(activity, 1, response.body().getData().getPhone());
                    getUserInfo();
//                }
//
//                @Override
//                public void onFailed(int code) {
//                    DialogHelper.dismissProgressDialog();
//                }
//
//                @Override
//                public void onException(Throwable exception) {
//                    DialogHelper.dismissProgressDialog();
//                }
//            });
        }
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) {
                    return;
                }
                if ("ok".equals(response.body().getState())) {
                    activity.getUserMsgSucceed(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void saveUserInfo(LoginEntity loginEntity) {
        repertory.saveToken(loginEntity.getToken());
        repertory.saveAccount(loginEntity.getPhone());

//        NimUIKit.setAccount(loginEntity.getImAccount());
//        AVChatKit.setAccount(loginEntity.getImAccount());
        //Preferences.saveUserAccount(loginEntity.getImAccount());
 //       Preferences.saveUserToken(loginEntity.getImToken());
    }

    public void follow(boolean isAttention,String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().follow(isAttention,no,5).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.follow();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
